<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\model\User as UserModel;
use app\api\model\Order as OrderModel;
use app\api\model\Setting as SettingModel;
use app\api\model\user\MoneyLog as MoneyLogModel;
use app\common\library\helper;
use app\common\model\Wxapp as WxappModel;

/**
 * 个人中心主页
 * Class Index
 * @package app\api\controller\user
 */
class Index extends Controller
{

    /**
     * 获取当前用户信息
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $user = $this->getUser(false);
        if($user){
            //查出用户的所有记录id
            $receivesIds=db('divide_receives')->where(['user_id'=>$user['user_id'],'wxapp_id'=>$user['wxapp_id']])->column('receives_id');
            $userRefundTotalMoney = 0;
            if(is_array($receivesIds)){

                //当是数组的时候
                if(is_array($receivesIds)){
                    //循环记录id，查询分账记录详情，
                    foreach ($receivesIds as $value){
                        $userExtends=db('divide_receives_user')->where(['receives_id'=>$value])->value('extend');
                        if ($userExtends){

                            //将json数据格式转化成数组
                            $data = json_decode($userExtends, true);
                            //循环转化的数组，拿到type==1的退款金额
                            foreach ($data as $item){
                                if($item['type']==1){
                                    //$userRefundTotalMoney +=  $item['refund_fee'];
                                    $userRefundTotalMoney =bcadd($userRefundTotalMoney,$item['refund_fee'],2)  ;

                                }
                            }
                        }else{
                            $userRefundTotalMoney +=0;

                        }
                    }
                }else{
                    //非数组的时候
                    $userExtends=db('divide_receives_user')->where(['receives_id'=>$receivesIds])->value('extend');
                    if($userExtends){

                        //将json数据格式转化成数组
                        $data = json_decode($userExtends, true);
                        //循环转化的数组，拿到type==1的退款金额
                            foreach ($data as $item){
                                if($item['type']==1){
                                   // $userRefundTotalMoney +=  $item['refund_fee'];
                                    $userRefundTotalMoney =bcadd($userRefundTotalMoney,$item['refund_fee'],2)  ;

                                }
                            }
                    }else{

                        $userRefundTotalMoney +=0;
                    }
                }
            }
            $dealerMoney = db('dealer_capital')
                ->where(['user_id'=>$user['user_id'],'wxapp_id'=>$user['wxapp_id']])
                ->where("flow_type!=60 and flow_type!=70")
                ->sum('money');

            if($dealerMoney==0||$dealerMoney==''){
                //$bonus =round($userRefundTotalMoney,2);
                $bonus =$userRefundTotalMoney;
            }else{
                $bonus =bcadd($userRefundTotalMoney, $dealerMoney,2);
            }
            $user['bonus'] =$bonus ? $bonus : 0;

        }
        $menus = (new UserModel)->getMenus();
       

        foreach ($menus as $k=>&$v){
            $v['is_show'] = true;

            if($k=="gifts"){
                $v['is_show'] = $user['shareholder']?true:false;

            }


        }

        // 订单总数
        $model = new OrderModel;

        return $this->renderSuccess([
            'userInfo' => $user,
            'orderCount' => [
                'payment' => $model->getCount($user, 'payment'),
                'received' => $model->getCount($user, 'received'),
                'comment' => $model->getCount($user, 'comment'),
            ],
            'setting' => [
                'points_name' => SettingModel::getPointsName(),
            ],
            'menus' => $menus   // 个人中心菜单列表
        ]);
    }

    /**
     * 获取当前用户钱包
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function wallet()
    {
        // 当前用户信息
        $user = $this->getUser(false);
        $data['user_id'] = $user['user_id'];//用户id
        $data['money'] = $user['money'];//余额
        $money = db('dealer_user')->where(['user_id'=>$user['user_id'],'is_delete'=>0])->sum('money')?:0;
        $freeze_money = db('dealer_user')->where(['user_id'=>$user['user_id'],'is_delete'=>0])->sum('freeze_money')?:0;
        $data['dealer'] = helper::bcadd($money,$freeze_money);//赏金
        $data['balance'] = $user['balance'];//零钱
        $data['points'] = $user['points'];//积分
        return $this->renderSuccess($data,'查询成功');

    }


    /**
     * 设置支付密码
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function payPass()
    {
        $password = input('password');
        $setpassword = input('setpassword');
        if(!trim($password)){
            return $this->renderError('请输入密码');
        }
        if(trim($password)!=trim($setpassword)){
            return $this->renderError('两次密码不一致,请重新输入');
        }
        // 当前用户信息
        $user = $this->getUser(false);
        $flag = db('user')->where(['user_id'=>$user['user_id']])->update(['pay_password'=>yoshop_hash($password),'payfailure'=>0]);
        if ($flag) {
            return $this->renderSuccess([], '设置成功');
        }
        return $this->renderError('新密码不能与近期密码一样');
    }


    /**
     * 购买输入支付密码
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function checkPass()
    {
        $password = $this->request->post()['password'];
        if(!trim($password)){
            return $this->renderError('请输入密码');
        }
        // 当前用户信息
        $user = $this->getUser(false);
        if(!$user['pay_password']){
            return $this->renderError('请先设置支付密码');
        }
        if($user['pay_password']!=yoshop_hash($password)){
            return $this->renderError('支付密码错误');
        }
        return $this->renderSuccess([],'密码正确');

    }


    /**
     * 判断是不是设置密码了
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function existPass()
    {
        // 当前用户信息
        $user = $this->getUser(false);
        if(!$user['pay_password']){
            return $this->renderSuccess(0,'未设置密码');
        }else{
            return $this->renderSuccess(1,'已设置密码');
        }
    }

}
